local cuancu = fk.CreateSkill {
  name = "3s__cuancu",
}

Fk:loadTranslationTable{
  ["3s__cuancu"] = "攒簇",
  [":3s__cuancu"] = "每轮开始时，你可分配至多三张【毒】给等量名角色。因此获得牌的角色造成或受到火焰伤害后，其可将一张【毒】当【桃】或【无中生有】使用，且防止其因此失去体力。",
  
  ["#3s__cuancu-distribute"] = "攒簇：分配三张【毒】给任意角色",
  ["#3s__cuancu-ex_nihilo"] = "攒簇：选择一张【毒】当【桃】或【无中生有】使用",
  ["@@cuancu-round"] = "攒簇",
  ["#3s__cuancu_prevent"] = "%from 因 %arg 效果防止失去体力",
  
  ["$3s__cuancu1"] = "毒以攻疾，药以养元。",
  ["$3s__cuancu2"] = "聚毒成势，化险为夷。",
}

-- 每轮开始时分配三张毒
cuancu:addEffect(fk.RoundStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(cuancu.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 印三张毒牌
    local poisons = {}
    for i = 1, 3 do
      table.insert(poisons, room:printCard("lvshi_poison", Card.NoSuit, 0).id)
    end
    
    -- 使用askToYiji分配三张毒，并获取分配结果
    local distribution_result = room:askToYiji(player, {
      cards = poisons,
      min_num = 1,
      max_num = 3,
      single_max = 1,
      targets = room:getAlivePlayers(),
      skill_name = cuancu.name,
      prompt = "#3s__cuancu-distribute",
      cancelable = true,
      expand_pile = poisons,
    })
    -- 记录获得毒的角色列表
    if distribution_result then
      --local recipients = {}
      for target, cards in pairs(distribution_result) do
        if #cards > 0 then
          room:addPlayerMark(room:getPlayerById(target), "@@cuancu-round")
          for _, card_id in ipairs(cards) do
            local card = Fk:getCardById(card_id)
            if card and card.name == "lvshi_poison" then
              room:addCardMark(card, "cuancu__destr_discard")
            end
          end
        end
      end
    end
  end,
})

-- 火焰伤害后的处理
cuancu:addEffect(fk.Damage, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(cuancu.name, true, true) and target:getMark("@@cuancu-round") > 0 and data.damageType == fk.FireDamage
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(target, "cuancu_prevent")
    if room.current and room.current:getMark("@@cuancu-round") > 0 then
      room:addPlayerMark(room.current, "cuancu_prevent")
    end
    room:askToUseVirtualCard(target, {
      name = {"peach", "ex_nihilo"},
      skill_name = cuancu.name,
      prompt = "#3s__cuancu-ex_nihilo",
      cancelable = true,
      card_filter = {
        n = 1,
        pattern = "lvshi_poison",
      },
    })
    room:setPlayerMark(target, "cuancu_prevent", 0)
    if room.current and room.current:getMark("@@cuancu-round") > 0 then
      room:setPlayerMark(room.current, "cuancu_prevent", 0)
    end
  end,
})

cuancu:addEffect(fk.Damaged, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(cuancu.name, true, true) and target:getMark("@@cuancu-round") > 0 and data.damageType == fk.FireDamage
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(target, "cuancu_prevent")
    if room.current and room.current:getMark("@@cuancu-round") > 0 then
      room:addPlayerMark(room.current, "cuancu_prevent")
    end
    room:askToUseVirtualCard(target, {
      name = {"peach", "ex_nihilo"},
      skill_name = cuancu.name,
      prompt = "#3s__cuancu-ex_nihilo",
      cancelable = true,
      card_filter = {
        n = 1,
        pattern = "lvshi_poison",
      },
    })
    room:setPlayerMark(target, "cuancu_prevent", 0)
    if room.current and room.current:getMark("@@cuancu-round") > 0 then
      room:setPlayerMark(room.current, "cuancu_prevent", 0)
    end
  end,
})

-- 防止因毒失去体力
cuancu:addEffect(fk.PreHpLost, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(cuancu.name, true, true) and data.who and data.who:getMark("@@cuancu-round") > 0
    and data.who:getMark("cuancu_prevent") > 0
  end,
  on_use = function(self, event, target, player, data)
    -- 使用preventHpLost方法正确防止失去体力
    player.room:sendLog{
      type = "#3s__cuancu_prevent",
      from = data.who.id,
      arg = cuancu.name,
      toast = true,
    } 
    data:preventHpLost()
  end,
})

return cuancu